﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Brand
/// </summary>
/// 
namespace Commercial
{
    public class Brand
    {
        Up1VNDataContext db = new Up1VNDataContext();
        public Brand()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public IList<brand> ListBrand()
        {
            var query = (from g in db.brands
                         where g.active == 1
                         select g);
            return query.ToList();
        }

        public void DeleteBrand(int id)
        {
            var query = (from g in db.brands
                         where g.id == id
                         select g).FirstOrDefault();
            query.active = 0;
            db.SubmitChanges();
        }

        /*
            isVip: 1:doanh nghiệp vip, 0:doanh nghiệp thường
            isCheck: -1: tạm ngưng, 0:đang chờ duyệt, 1:duyệt
        */
        public int CreateBrand(string name, string description, string logo, string url, string category_job, string customer_brand, string addres, string phone, string mobile, string email, string fax, string tax_code, int isVip, int isCheck, int id_city, int id_district)
        {
            brand brand = new brand();
            brand.name = name;
            brand.description = description;
            brand.logo = logo;
            brand.url = url;
            brand.category_job = category_job;
            brand.customer_brand = customer_brand;
            brand.address = addres;
            brand.brand_user = Helper.ConvertToUnSign(name);
            brand.brand_pass = "111111";
            brand.phone = phone;
            brand.mobile = mobile;
            brand.email = email;
            brand.fax = fax;
            brand.tax_code = tax_code;
            brand.active = 1;
            brand.isVip = isVip;
            brand.isCheck = isCheck;
            brand.id_city = id_city;
            brand.id_district = id_district;
            brand.date_created = DateTime.Now;
            db.brands.InsertOnSubmit(brand);
            db.SubmitChanges();
            return brand.id;
        }

        public brand DetailBrand(int id)
        {
            var query = (from g in db.brands
                         where g.id == id
                         select g).FirstOrDefault();
            return query;
        }

        public void UpdateBrand(int id, string name, string description, string logo, string url, string category_job, string customer_brand, string address, string phone, string mobile, string email, string fax, string tax_code, int isVip, int isCheck, int id_city, int id_district)
        {
            var query = DetailBrand(id);
            query.name = name;
            query.description = description;
            query.logo = logo;
            query.url = url;
            query.category_job = category_job;
            query.customer_brand = customer_brand;
            query.address = address;
            query.phone = phone;
            query.mobile = mobile;
            query.email = email;
            query.fax = fax;
            query.tax_code = tax_code;
            query.isVip = isVip;
            query.isCheck = isCheck;
            query.id_city = id_city;
            query.id_district = id_district;
            db.SubmitChanges();
        }

        public int CheckExistBrand(string name)
        {
            var query = (from g in db.brands
                         where g.name == name
                         select g);
            int row = query.Count();
            if (row >= 1)
                return row;
            return 0;
        }

    }
}